/*     */ package com.cr.api.controller;
/*     */ 
/*     */ import com.cr.api.model.AddAnswerModel;
/*     */ import com.cr.api.model.AddQuestionModel;
/*     */ import com.cr.api.vo.AnswerVo;
/*     */ import com.cr.api.vo.QuestionVo;
/*     */ import com.cr.business.domain.CrAnswer;
/*     */ import com.cr.business.domain.CrQuestion;
/*     */ import com.cr.business.domain.CrUser;
/*     */ import com.cr.business.dto.UserAanwerDto;
/*     */ import com.cr.business.service.ICrAnswerService;
/*     */ import com.cr.business.service.ICrQuestionService;
/*     */ import com.cr.business.service.ICrUserService;
/*     */ import com.cr.common.core.controller.BaseController;
/*     */ import com.cr.common.core.domain.AjaxResult;
/*     */ import com.cr.common.utils.ConvertUtil;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.github.pagehelper.PageInfo;
/*     */ import java.util.*;
/*     */
/*     */
/*     */
import java.util.stream.Collectors;
/*     */ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponses;

import org.springframework.beans.BeanUtils;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.web.bind.annotation.*;
/*     */
/*     */
/*     */

import javax.validation.constraints.NotNull;

/*     */
          @Api(description = "法律问答")
/*     */ @RestController
/*     */ @RequestMapping({"/api/question"})
          @CrossOrigin
/*     */ public class ApiQuestionController extends BaseController {
/*     */   @Autowired
/*     */   private ICrQuestionService questionService;
/*     */   
/*     */   @Autowired
/*     */   private ICrAnswerService answerService;
/*     */   
/*     */   @Autowired
/*     */   private ICrUserService userService;
/*     */
            /**
             * 问题列表
             */
            @ApiOperation("问题列表")
            //@ApiResponses(
            //        @ApiResponse(code=0,message = "操作成功", response = QuestionVo.class)
            //)
            @PostMapping("/getQuestionList")
            @ResponseBody
            public AjaxResult getNoticeList()
            {
                CrQuestion question = new CrQuestion();
                question.setScreenStatus(0);
                question.setExamineStatus(1);//已审核
                startPage();
                List<CrQuestion> questionList = questionService.selectCrQuestionListFulltext(question);
                List<QuestionVo> questionVoList = new ArrayList<>();
                if(questionList.size() > 0){
                    questionVoList = ConvertUtil.convertList(questionList, QuestionVo.class);
                    for(QuestionVo vo : questionVoList){
                        CrUser user = userService.selectCrUserByUserId(vo.getUserId());
                        if(user != null){
                            vo.setHeadPortrait(user.getHeadPortrait());
                        }
                        List<UserAanwerDto> userAanwerDtoList = answerService.selectAnswerUser(vo.getQuestionId());
//                        if(userAanwerDtoList.size() > 0){
//                            List<String> headPortraitList = userAanwerDtoList.stream().map(v->v.getHeadPortrait()).collect(Collectors.toList());
//                            if(headPortraitList.size() > 3){
//                                vo.setUserList(headPortraitList.subList(0,3));
//                            } else {
//                                vo.setUserList(headPortraitList);
//                            }
//                        }
                        vo.setUserNumber(userAanwerDtoList.size());
                    }
                }
                return AjaxResult.success(new PageInfo<>(questionVoList));
            }

/*     */   
/*     */   @PostMapping({"/addQuestion"})
/*     */   @ResponseBody
            @ApiOperation("法律问答-提问")
/*     */   public AjaxResult addQuestion(@RequestBody AddQuestionModel model) {
/* 123 */     if (model.getUserId() == null)
/* 124 */       return AjaxResult.error("用户id不能为空"); 
/* 126 */     if (MyStringUtils.isEmpty(model.getContent()))
/* 127 */       return AjaxResult.error("问题详情不能为空"); 
/* 129 */     CrQuestion question = new CrQuestion();
/* 130 */     BeanUtils.copyProperties(model, question);
/* 132 */     question.setScreenStatus(Integer.valueOf(0));
/* 133 */     question.setCreateBy(model.getUserId().toString());
/* 134 */     question.setCreateTime(new Date());
/* 135 */     return toAjax(this.questionService.insertCrQuestion(question));
/*     */   }
/*     */   
            /**
             * 回复
             * @param model
             * @return
             */
            @ApiOperation("回复")
            @PostMapping("/addAnswer")
            @ResponseBody
            public AjaxResult addAnswer(@RequestBody AddAnswerModel model){
                if(model.getUserId() == null){
                    return AjaxResult.error("用户id不能为空");
                }
                if(model.getQuestionId() == null){
                    return AjaxResult.error("问题id不能为空");
                }
                if(MyStringUtils.isEmpty(model.getAnswerContent())){
                    return AjaxResult.error("回答内容不能为空");
                }
                CrAnswer answer = new CrAnswer();
                BeanUtils.copyProperties(model, answer);
                answer.setScreenStatus(0);
                answer.setCreateBy(model.getUserId().toString());
                answer.setCreateTime(new Date());
                if(answer.getAnswerType()!= null && answer.getAnswerType()==1) {
                    answer.setPayResult(1);
                    answer.setParentAnswerId(model.getParentAnswerId());
                }
                Integer i = answerService.insertCrAnswer(answer);
                System.out.println("主键："+answer.getAnswerId());
                if(i > 0){
                    //增加回复数量
                    CrQuestion question = questionService.selectCrQuestionByQuestionId(model.getQuestionId());
                    question.setRepliesNumber(question.getRepliesNumber() + 1);
                    questionService.updateCrQuestion(question);
                }
                if(answer.getAnswerType()!= null && answer.getAnswerType()==1) {
                    return toAjax(i);
                }
                return AjaxResult.success(answer.getAnswerId());
            }
            
            /**
             * 增加阅读数
             * @param questionId
             */
            @ApiOperation("增加阅读数")
            @ApiImplicitParam(name = "questionId", value = "问题ID", required = true, dataType = "int")
            @PostMapping("/addReadNumber")
            @ResponseBody
            public AjaxResult addReadNumber(@RequestBody CrQuestion param){
                Long questionId = param.getQuestionId();
                if(questionId != null){
                    CrQuestion question = questionService.selectCrQuestionByQuestionId(questionId);
                    question.setReadNumber(question.getReadNumber() + 1);
                    questionService.updateCrQuestion(question);
                }
                return AjaxResult.success();
            }
            
            /**
             * 问题详情
             * @param questionId
             * @return
             */
            @ApiOperation("问题详情")
//            @ApiImplicitParam(name = "questionId", value = "问题ID", required = true, dataType = "int")
            @PostMapping("/getInfo")
            @ResponseBody
            public AjaxResult getInfo(@RequestBody CrQuestion param){
                Long questionId = param.getQuestionId();
                if(questionId == null){
                    return AjaxResult.error("问题id不能为空");
                }
                CrQuestion question = questionService.selectCrQuestionByQuestionId(questionId);
                if(question == null){
                    return AjaxResult.error("问题不存在");
                }
                question.setReadNumber(question.getReadNumber() + 1);
                questionService.updateCrQuestion(question);
                CrAnswer answer = new CrAnswer();
                answer.setQuestionId(questionId);
                if(param.getAnswerType() == 1) {
                    answer.setParentAnswerId(param.getParentAnswerId());
                }else {
                    answer.setParentAnswerId(null);
                }
                answer.setScreenStatus(0);
                answer.setAnswerType(param.getAnswerType());
                answer.setExamineStatus(1);
                startPage();
                List<CrAnswer> answerList = answerService.selectCrAnswerList(answer);
                List<AnswerVo> answerVoList = new ArrayList<>();
                if(answerList.size() > 0){
                    answerVoList = ConvertUtil.convertList(answerList, AnswerVo.class);
//                    for(AnswerVo vo : answerVoList){
//                        if(vo.getAnswerType() != null && vo.getPayResult() != null) {
//                            if("1".equals(vo.getAnswerType().toString()) && !"0".equals(vo.getPayResult().toString())) {
//                                return AjaxResult.error("请支付后查看");
//                            }
//                        }
//                        CrAnswer crAnswer = new CrAnswer();
//                        crAnswer.setQuestionId(questionId);
//                        crAnswer.setScreenStatus(0);
//                        crAnswer.setParentAnswerId(vo.getAnswerId());
//                        List<CrAnswer> crAnswerList = answerService.selectCrAnswerList(crAnswer);
//                        List<AnswerVo.Answer> answers = ConvertUtil.convertList(crAnswerList, AnswerVo.Answer.class);
//                        vo.setAnswerList(answers);
                   // }
                }
                return AjaxResult.success(new PageInfo<>(answerVoList));
            }

/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-api-4.7.0.jar!\com\cr\api\controller\ApiQuestionController.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */